<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mews\Captcha\Facades\Captcha;


/**
 * 登录相关
 * Class CateController
 * @package App\Http\Controllers
 */
class AuthController extends Controller {


    public function login(Request $request){
        $data = $request->all();
        $check = Captcha::check_api($data['captcha'],$data['captchaKey']);
        if(!$check) return $this->writeJson(1,null,'验证码错误');
        $credentials = [
            'username' => $data['username'],
            'password' => $data['password'],
        ];
        $token = auth('admin')->attempt($credentials);
        if ($token) {
            $user = auth('admin')->user();
            $login = [
                'user' => ['username' => $user['nickname']],
                'access_token' => $token,
                'token_type' => 'bearer',
                'expires_in' => auth('admin')->factory()->getTTL() * 60
            ];
            return $this->writeJson(0,$login,'登录成功，页面即将跳转...');
        }
        return $this->writeJson(1,null,'账户或者密码错误');
    }



    public function captcha(){
        $captcha = Captcha::create('default', true);
        return $this->writeJson(0,$captcha);
    }


}
